package com.example.demo.hot100;

/**
 * @Classname no12
 * @Description
 * @Date 2023/12/21 9:57
 * @Created by jyl
 *
 * 这个方法的思想是贪心法，每次选择尽可能大的罗马数字单位，依次减去直到数字为0。
 * values 数组存储了各个罗马数字单位对应的整数值，而 symbols 数组存储了对应的罗马数字。
 *
 * 将整数转换为罗马数字。
 */
public class no12 {

    public static String intToRoman(int num) {
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < values.length; i++) {
            while (num >= values[i]) {
                num -= values[i];
                result.append(symbols[i]);
            }
        }

        return result.toString();
    }

    public static void main(String[] args) {

        // Example usage:
        int num = 1994;
        String romanNumeral = intToRoman(num);
        System.out.println("Roman numeral for " + num + " is: " + romanNumeral);
    }
}
